package com.hk.userservice.service.impl;

import com.github.pagehelper.PageHelper;
import com.hk.model.LoginLog;
import com.hk.userservice.mapper.LoginLogMapper;
import com.hk.userservice.service.LoginLogService;
import com.hk.userservice.service.es.EsLoginLogService;
import com.hk.userservice.utils.EsUtil;
import com.hk.utlis.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author zhe
 * @Create 2021/7/30/14:09
 * @State 登录日志服务接口实现类
 */
@Slf4j
@Service
public class LoginLogServiceImpl implements LoginLogService {

    @Autowired
    private LoginLogMapper loginLogMapper;

    //    Es
    @Autowired
    private EsLoginLogService esLoginLogService;

    @Override
    public List<LoginLog> findAll(Integer page, Integer limit, LoginLog loginLog) {
        PageHelper.startPage(page,limit);
        List<LoginLog> loginLogList = loginLogMapper.findAll(loginLog);
        return loginLogList;
    }

    @Override
    public int count(Integer page, Integer limit, LoginLog loginLog) {
        int count = loginLogMapper.count(loginLog);
        return count;
    }

    @Override
    public LoginLog findById(Integer id) {
        LoginLog loginLog = loginLogMapper.findById(id);
        return loginLog;
    }

    @Override
    public int save(LoginLog loginLog) {
        //        当前时间
        String dateTimeString = DateUtils.dateTimeString(new Date());
        loginLog.setGmtCreate(dateTimeString);
        int save = loginLogMapper.save(loginLog);
        if (save == 1) {
            log.error("账户登录日志已保存！");
        }
        return save;
    }

    /**
     * 在保证数据库的修改成功下，修改Es 保证数据真实性
     * @param loginLog
     * @return
     */
    @Transactional
    @Override
    public int update(LoginLog loginLog) throws IOException {
//        当前时间
        String dateTimeString = DateUtils.dateTimeString(new Date());
        loginLog.setGmtModified(dateTimeString);
        int update = loginLogMapper.update(loginLog);
        if (update == 1) {
            log.error("账户退出时间已保存！");
            //        Es 保存当前登录账户退出时间和状态
            esLoginLogService.updateDocumentInfo(EsUtil.ES_INDEX,String.valueOf(loginLog.getId()),loginLog,1l);
        }
        return update;
    }

    /**
     *
     * @param id
     * @return
     */
    @Override
    public int delete(Integer id) throws IOException {
        int delete = loginLogMapper.delete(id);
        if (delete == 1) {
            log.error("Id为"+id+"的登录日志删除成功！");;
        }
        return delete;
    }

    @Transactional
    @Override
    public int deleteLimit(Integer count) throws IOException {
        int deleteLimit = loginLogMapper.deleteLimit(count);
        /*if (deleteLimit != 0) {
            log.error("登录日志已删除"+deleteLimit+"条");
//            删除索引
            esLoginLogService.deleteIndex(EsUtil.ES_INDEX,10L);
        }*/
        return deleteLimit;
    }
}
